package cc.comac.mom.service.impl;

import cc.comac.mom.bean.EquipMaintainWorkOrderBean;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.datahandlers.entity.QualityAttachmentGroupDataHandler;
import cc.comac.mom.redis.IdFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipMaintainWorkOrderExecutionService;
import cc.comac.mom.bean.EquipMaintainWorkOrderExecution;
import cc.comac.mom.datahandlers.entity.EquipMaintainWorkOrderExecutionDataHandler;
import cc.comac.mom.util.ResultBean;

import javax.annotation.Resource;
import java.util.List;

@Service
@RestController
public class EquipMaintainWorkOrderExecutionServiceImpl implements EquipMaintainWorkOrderExecutionService{

	@Autowired
	private EquipMaintainWorkOrderExecutionDataHandler equipMaintainWorkOrderExecutionDataHandler;
	@Resource
	private IdFactory idFactory;
	@Autowired
	private QualityAttachmentGroupDataHandler qualityAttachmentGroupDataHandler;
	@Override
	public ResultBean updateMaintainWorkOrderExecution(
			@RequestBody EquipMaintainWorkOrderExecution equipMaintainWorkOrderExecution) {
		ResultBean updateMaintainWorkOrderExecution = equipMaintainWorkOrderExecutionDataHandler.updateMaintainWorkOrderExecution(equipMaintainWorkOrderExecution);
		return updateMaintainWorkOrderExecution;
	}

	@Override
	public ResultBean selectByOrderId(@RequestBody EquipMaintainWorkOrderExecution equipMaintainWorkOrderExecution) {
		ResultBean selectByOrderId = equipMaintainWorkOrderExecutionDataHandler.selectByOrderId(equipMaintainWorkOrderExecution);
		return selectByOrderId;
	}

	@Override
	public ResultBean selectByCondition(@RequestBody EquipMaintainWorkOrderBean equipMaintainWorkOrderExecution) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev selectByCondition = equipMaintainWorkOrderExecutionDataHandler.selectByCondition(equipMaintainWorkOrderExecution);
		resultBean.setObject(selectByCondition);
		return resultBean;
	}

	@Override
	public ResultBean startExecution(@RequestBody EquipMaintainWorkOrderExecution equipMaintainWorkOrderExecution) {
		ResultBean startExecution = equipMaintainWorkOrderExecutionDataHandler.startExecution(equipMaintainWorkOrderExecution);
		return startExecution;
	}

	@Override
	public ResultBean executionBenchmark(@RequestBody EquipMaintainWorkOrderExecution obj) {

		if (obj != null && obj.getInnerData() != null) {
			List<QualityAttachmentGroup> listAttachmentGroup = obj.getListAttachmentGroup();
			if (listAttachmentGroup != null && listAttachmentGroup.size() > 0) {
				long attachmentGroupId = obj.getInnerData().getPictureAttachmentGroupId();
				if (attachmentGroupId == 0) {
					long id = idFactory.generateId();
					obj.getInnerData().setPictureAttachmentGroupId(id);
					for (QualityAttachmentGroup item : listAttachmentGroup) {
						item.getInnerData().setGroupId(id);
					}
				} else {
					for (QualityAttachmentGroup item : listAttachmentGroup) {
						item.getInnerData().setGroupId(attachmentGroupId);
					}
				}
				ResultBean resultBean2 = qualityAttachmentGroupDataHandler.insertBatch(listAttachmentGroup);
				if (!resultBean2.success) {
					throw new RuntimeException("保存附件组信息失败");
				}
			}

		}
		ResultBean executionBenchmark = equipMaintainWorkOrderExecutionDataHandler.executionBenchmark(obj);
		return executionBenchmark;
	}

}
